package com.example.music;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.example.filter.MusicFilter;
/**
 * 音乐播放的点击事件的实现
 * @author develop
 *
 */
public class MusicPlayer extends ListActivity{
	private MediaPlayer mediaPlayer;
	private List<String> musicList=new ArrayList<String>();
	private static final String MusicPath="/sdcard/";
	private String btn_id;
	private boolean flag=true;
	private int currentMusicItem=0;
	private String TAG="MusicPlayer";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mediaPlayer=new MediaPlayer();
		musicList();
		newIntent();
	}
	private void newIntent() {
		// TODO Auto-generated method stub
		IntentFilter intentfilter=new IntentFilter();
		intentfilter.addAction("intent");
		BroadcastReceiver receiver=new BroadcastReceiver() {
			
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				btn_id=intent.getStringExtra("btnFlag");
			}
		};
		registerReceiver(receiver, intentfilter);
		if(btn_id.equals("lastOne")){
			lastMusic();
		}else if(btn_id.equals("pauseOrCreate")){
			if(mediaPlayer.isPlaying()){
				if(flag==true){
					mediaPlayer.pause();
					flag=false;
				}else{
					mediaPlayer.start();
					flag=true;
				}
			}else{
				playMusic(MusicPath+musicList.get(currentMusicItem));
			}
			
			
		}else if(btn_id.equals("nextOne")){
			nextMusic();
		}
	}
	public void playMusic(String path) {
		// TODO Auto-generated method stub
		Log.i(TAG, "playMusic");
		try{
			mediaPlayer.reset();
			mediaPlayer.setDataSource(path);
			mediaPlayer.prepare();
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub
					nextMusic();
				}
			});
		}catch (Exception e){
			e.printStackTrace();
		}
		
		
		
	}
	public void nextMusic() {
		// TODO Auto-generated method stub
		Log.i(TAG, "nextMusic");
		if(++currentMusicItem>musicList.size()){
			currentMusicItem=0;
		}else{
			playMusic(MusicPath+musicList.get(currentMusicItem));
		}
	}
	private void lastMusic() {
		// TODO Auto-generated method stub
		Log.i(TAG, "lastMusic");
		if(--currentMusicItem>=musicList.size()){
			currentMusicItem=0;
		}else{
			playMusic(MusicPath+musicList.get(currentMusicItem));
		}
		
	}
	private void musicList() {
		// TODO Auto-generated method stub
		File home=new File(MusicPath);
		if(home.listFiles(new MusicFilter()).length>0){
			for(File file:home.listFiles(new MusicFilter())){
				musicList.add(file.getName());
			}
		}
		ArrayAdapter<String> list=new ArrayAdapter<String>(this, R.layout.musicitem, musicList);
		setListAdapter(list);
	}
	

}
